<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'直播列表'),
	array('op'=>'edit','title'=>'添加直播'),	
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['merchid'] = $merchid;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['storeid'])){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['workerid'])){
		$params['workerid']=intval($_RQ['workerid']);
		$return['params']['workerid']=$_RQ['workerid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_yiliao_zhibo',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['starttime']=$this->model('lesson')->get_uptime($item['starttime']);
			$item['endtime']=$this->model('lesson')->get_uptime($item['endtime']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'tip'=>'报名直播','list'=>array(
			array('name'=>'title','title'=>'直播名称','type'=>'string'),
			array('name'=>'image','title'=>'直播图片','type'=>'image'),
			array('name'=>'workerid','title'=>'直播讲师','type'=>'select','options'=>'workers','option_title'=>'name'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'action'=>array(
		)),
		'search'=>array(
			array('name'=>'workerid','width'=>10,'title'=>'直播讲师','type'=>'select','options'=>'workers','option_title'=>'name'),
			array('name'=>'title','width'=>10,'title'=>'直播名称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	$return['stores']=pdo_getall('jy_yiliao_store',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','title'),'id');
	$return['areas']=pdo_getall('jy_yiliao_area',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','title'),'id');
	$return['workers']=pdo_getall('jy_yiliao_worker',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','name'),'id');
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_yiliao_zhibo',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['starttime']=date('Y-m-d',$item['starttime']);
		$item['endtime']=date('Y-m-d',$item['endtime']);
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回直播列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'title','title'=>'直播名称','type'=>'string'),
					array('name'=>'workerid','title'=>'直播讲师','type'=>'select','options'=>'workers','option_title'=>'name'),
					array('name'=>'price','title'=>'直播价格','type'=>'string'),
					array('name'=>'liveurl','title'=>'直播链接','type'=>'string'),
					array('type'=>'time','title'=>'开始时间','name'=>'starttime','help'=>''),
					array('type'=>'time','title'=>'结束时间','name'=>'endtime','help'=>''),
					array('name'=>'logo','title'=>'logo','type'=>'image','help'=>'建议尺寸：640宽*220高'),
					array('name'=>'image','title'=>'展示图片','type'=>'image','help'=>'建议尺寸：640宽*220高'),
					array('name'=>'description','title'=>'简要描述','type'=>'text'),
					array('name'=>'content','title'=>'详细介绍','type'=>'richtext'),
					array('name'=>'paixu','title'=>'排序','type'=>'string'),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
				)
			),
		)
	);
	$return['storetype']=$this->storetype;
	$return['htmls']=$htmls;
	$return['areas']=pdo_getall('jy_yiliao_area',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','title'),'id');
	$return['stores']=pdo_getall('jy_yiliao_store',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','title'),'id');
	$return['workers']=pdo_getall('jy_yiliao_worker',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','name'),'id');
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写直播名称！','error');
	}
	$postdata['merchid'] = $merchid;
	$data=array_fields(array('liveurl','price','title','logo','starttime','endtime','area_id','workerid','storeid','image','content','description','time','on_num','paixu','status','merchid'),$postdata);
	$id=intval($postdata['id']);
	$data['starttime']=strtotime($data['starttime']);
	$data['endtime']=strtotime($data['endtime']);
	if($id){
		pdo_update('jy_yiliao_zhibo',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_yiliao_zhibo',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['area_id'])){
		$params['area_id']=intval($_RQ['area_id']);
		$return['params']['area_id']=$_RQ['area_id'];
	}
	if(!empty($_RQ['storeid'])){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['groupid'])){
		$params['groupid']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];
	}
	if(!empty($_RQ['workerid'])){
		$params['workerid']=intval($_RQ['workerid']);
		$return['params']['workerid']=$_RQ['workerid'];
	}
	$lesson=pdo_getall('jy_yiliao_zhibo',$params);
	foreach($lesson as &$v){
		$v['starttime']=date('Y-m-d',$v['starttime']);
		$v['endtime']=date('Y-m-d',$v['endtime']);
		$v['storetitle']=pdo_getcolumn('jy_yiliao_store',array('id'=>$v['storeid']),'title');
		$v['storetitle']=$v['storetitle']?:'无';
		$v['areatitle']=pdo_getcolumn('jy_yiliao_area',array('id'=>$v['area_id']),'title');
		$v['grouptitle']=pdo_getcolumn('jy_yiliao_zhibo_group',array('id'=>$v['groupid']),'title');
		$v['workertitle']=pdo_getcolumn('jy_yiliao_worker',array('id'=>$v['workerid']),'name');
	}
	$header=array(
		'id'=>'直播ID',
		'title'=>'直播名称',
		'grouptitle'=>'直播类型',
	);
	cfc('tools')->export2excel($header,$lesson,'直播表');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_yiliao_zhibo',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的直播！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_yiliao_zhibo',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}